//
//  VerifySMSController.m
//  LuCaiZhu
//
//  Created by Stella on 2018/9/25.
//

#import "Header.h"
#import "UIColor+NN.h"
#import "HandleNetLogin.h"
#import "HandleNetProduct.h"
#import "CKPuslichTool.h"
#import "UserModel.h"
#import "ValidateTools.h"
#import "TKAlertCenter.h"
#import "DBAcquireAuthCodeButton.h"
#import "VerifySMSController.h"

@interface VerifySMSController ()
@property (weak, nonatomic) IBOutlet UIView *bgView;
@property (weak, nonatomic) IBOutlet DBAcquireAuthCodeButton *sendButton;
@property (weak, nonatomic) IBOutlet UILabel *phoneTextLa;
@property (weak, nonatomic) IBOutlet UITextField *codeTextField;
@property (strong,nonatomic)NSDate *oldDate;
@property (strong,nonatomic)NSString *mCode;

@property (weak, nonatomic) IBOutlet UIButton *okButton;
@property (weak, nonatomic) IBOutlet UIView *emailView;
@property (weak, nonatomic) IBOutlet UITextField *emailTextField;
@property (weak, nonatomic) IBOutlet UITextField *emailTextField2;


@end

@implementation VerifySMSController

- (void)viewDidLoad {
    [super viewDidLoad];
    if (_isEmail) {
        _emailView.hidden = NO;
        _bgView.hidden = YES;
        [_emailTextField becomeFirstResponder];
    }else{
        _emailView.hidden = YES;
        _bgView.hidden = NO;
    }
    _emailTextField.text = _emailSt;
    UIViewSetRadius(_bgView, 5, 0, nil);
    UIViewSetRadius(_sendButton, 2, 0.5, MAIN_COLOR_Red);
    
    UIViewSetRadius(_emailView, 5, 0, nil);
    UIViewSetRadius(_okButton, 22, 0, nil);
    UserModel *user = [UserModel getUser];
    NSString *telNo=user.Phone;
    NSString *phone=[NSString stringWithFormat:@"%@****%@",[telNo substringToIndex:3],[telNo substringFromIndex:7]];
    _phoneTextLa.text = [NSString stringWithFormat:@"已向%@发送电子签约加密验证码",phone];
}

- (IBAction)sendEmailAddress:(id)sender {
    _okButton.enabled = NO;
    [_emailTextField resignFirstResponder];
    if (_emailTextField.text.length == 0) {
        [[TKAlertCenter defaultCenter]postAlertWithMessage:@"请输入邮箱地址"];
        return;
    }
    
    if (![ValidateTools validateEmail:_emailTextField.text]) {
        [[TKAlertCenter defaultCenter]postAlertWithMessage:@"请输入正确的邮箱地址"];
        return;
    }
    if (![_emailTextField.text isEqualToString:_emailTextField2.text]) {
        [[TKAlertCenter defaultCenter]postAlertWithMessage:@"两次输入的邮箱不一致!"];
        return;
    }
    NSLog(@"_bookingId --%@  _emailTextField.text == %@",_bookingId,_emailTextField.text);
    [HandleNetProduct sendEmailWithBookingID:_bookingId AndEmail:_emailTextField.text Success:^(NSDictionary *data) {
        _okButton.enabled = YES;
        [[TKAlertCenter defaultCenter]postAlertWithMessage:@"发送成功"];
        [self dismissViewControllerAnimated:YES completion:nil];
        if (self.returnVerifyOK) {
            self.returnVerifyOK();
        }
       
    } Failure:^(NSString *errorInfo) {
        _okButton.enabled = YES;
    }];
}


- (IBAction)sendCode:(id)sender {
    _sendButton.enabled = NO;
    [HandleNetLogin getCodeForPhone:[UserModel getUser].Phone Success:^(NSDictionary *data) {
        _sendButton.enabled = YES;
        _mCode = data[@"Verificationcode"];
        _oldDate=[NSDate date];
        [_sendButton tarStartButtonTimer];
        [_codeTextField becomeFirstResponder];
    } Failure:^(NSString *errorInfo) {
        _sendButton.enabled = YES;
    }];
   
    
}



- (IBAction)myButtonPress:(UIButton *)sender {
    [_codeTextField resignFirstResponder];
    if (sender.tag == 0) {
        [self dismissViewControllerAnimated:YES completion:nil];
        return;
    }
    
    if (self.codeTextField.text.length == 0) {
        [[TKAlertCenter defaultCenter]postAlertWithMessage:@"请输入验证码"];
        return;
    }
    if (![self.codeTextField.text isEqualToString:_mCode]) {

        [[TKAlertCenter defaultCenter]postAlertWithMessage:@"验证码错误，请重新输入"];
        return;
    }
    
    NSDate *currentDate=[NSDate date];
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    
    NSInteger interval = [zone secondsFromGMTForDate:currentDate];
    
    NSDate *localDate1 = [currentDate dateByAddingTimeInterval:interval];
    NSDate *localDate2 = [_oldDate dateByAddingTimeInterval:interval];
    
    //计算时间差间隔
    NSTimeInterval timeBetween = [localDate1 timeIntervalSinceDate:localDate2];
    NSLog(@"timeBetween---%f",timeBetween);
    if (timeBetween>60*10) {
        [[TKAlertCenter defaultCenter]postAlertWithMessage:@"验证码已失效"];
        return;
    }
    [self dismissViewControllerAnimated:YES completion:nil];
    if (self.returnVerifyOK) {
        self.returnVerifyOK();
    }
}

@end
